package com.nativetalk.service.three.clockin;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.clockin.TdNtClockIn;
import com.nativetalk.bean.member.TdNtMemberClcokIn;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.dao.TdNtClockDao;
import com.nativetalk.dao.TdNtMemberDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 16/8/26.
 */
@Service
public class ClockInServiceImpl implements  ClockInService {
    private static Logger logger = Logger.getLogger(ClockInServiceImpl.class);
    @Resource
    private TdNtClockDao tdNtClockDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Override
    public RetInfo findClockInData(int memb_id) {
        String logInfo = this.getClass().getName() + ":findClockInData:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            TdNtMemberClcokIn tdNtMemberClcokIn = tdNtMemberDao.findMemberClockInDay(memb_id);

            TdNtClockIn tdNtClockIn = new TdNtClockIn();
            tdNtClockIn.setClock_int_day(CommonUtil.getDay());
            tdNtClockIn.setMemb_id(memb_id);
            TdNtClockIn clockIn = tdNtClockDao.findTdNtClockIn(tdNtClockIn);
            if (clockIn != null){
                result.put("is_clock_in",0);
            } else {
                result.put("is_clock_in",1);
            }
            result.put("total_clock_in_day",tdNtMemberClcokIn.getTotal_clock_in_day());
            result.put("total_study_times",tdNtMemberClcokIn.getTotal_study_times());
            result.put("share_name","我在外教君已打卡" + tdNtMemberClcokIn.getTotal_clock_in_day() + 1 + "天");
            result.put("share_url", Constants.member_clockin_url + memb_id);
            result.put("share_picutre",Constants.DOMAIN+"nativetalk/WechatIMG2.png" );
            result.put("share_content","我在外教君已打卡" + tdNtMemberClcokIn.getTotal_clock_in_day() + 1 + "天");
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取打卡数据成功");
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    @Transactional
    public RetInfo clockIn(int memb_id) {
        String logInfo = this.getClass().getName() + ":clockIn:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtClockIn tdNtClockIn = new TdNtClockIn();
            tdNtClockIn.setClock_int_day(CommonUtil.getDay());
            tdNtClockIn.setMemb_id(memb_id);
            TdNtClockIn clockIn = tdNtClockDao.findTdNtClockIn(tdNtClockIn);
            if (clockIn != null) {
                clockIn.setClock_in_size(clockIn.getClock_in_size() + 1);
                tdNtClockDao.updateTdNtClockIn(clockIn);
            } else {
                tdNtClockIn.setClock_in_month(CommonUtil.getMonth(new Date()));
                tdNtClockIn.setClock_in_size(1);
                tdNtClockIn.setClock_int_day(CommonUtil.getDay());
                tdNtClockDao.insertTdNtClockIn(tdNtClockIn);
                TdNtMemberClcokIn tdNtMemberClcokIn = tdNtMemberDao.findMemberClockInDay(memb_id);
                tdNtMemberClcokIn.setTotal_clock_in_day(tdNtMemberClcokIn.getTotal_clock_in_day()  +1);
                tdNtMemberDao.updateMemberClockInDay(tdNtMemberClcokIn);
            }
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("打卡成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
